// components/authorize-btn/authorize-btn.js
import {getStorageSync} from "../../utils/Storage";
import {updateUsrInfo, getPhoneNumber} from "../../service/User";
import Toast from "@vant/weapp/toast/toast";
Component({
    /**
     * 组件的属性列表
     */
    properties: {
        show: {
            type: Boolean,
            value: true
        },
        checkPhoneNumber: { //是否验证用户注册
            type: Boolean,
            value: true
        },
        checkUserInfo: { //是否验用户授权
            type: Boolean,
            value: true
        },
        customClass: {
            type: String,
            value: ""
        },
        customStyle: {
            type: String,
            value: ""
        },
        position: { //绝对定位
            type: Boolean,
            value: false
        },
        round: {
            type: Boolean,
            value: false
        },
        type: {
            type: String,
            value: "info"
        },
        size: {
            type: String,
            value: "large"
        },
        color: {
            type: String,
            value: "var(--color-primary)"
        },
        icon: {
            type: String,
            value: ""
        },
        disabled: {
            type: Boolean,
            value: false
        },
        text: {
            type: String,
            value: "授权"
        },
        loading: {
            type: Boolean,
            value: false
        },
        loadingText: {
            type: String,
            value: "授权中.."
        },
        check: { //通知组件验证状态
            type: Boolean,
            value: false
        }
    },
    /**
     * 组件的初始数据
     */
    data: {
        _openType: ""
    },

    /*
    * 数据监听
    * */
    observers: {
        check: function (val) {
            if (val) {
                this.data._loadingText = this.data.loadingText; //保存初始状态
                this.checkAuthorizeStatus()
                this.setData({check: false}); //重置
            }
        },
        position: function (val) {
            if (val) {
                this.setData({
                    customStyle: "position: absolute; width:100%; height:100%; left:0; top:0; z-index:1; opacity: 0;" + this.data.customStyle
                })
            }
        }
    },

    /*
    * 组件所在页面的生命周期函数
    * */
    pageLifetimes: {
        // show: function () {
        //     const that = this;
        //     that.checkAuthorizeStatus();
        // },
    },

    /**
     * 组件的方法列表
     */
    methods: {

        /*
        * 验证会员授权状态
        * */
        checkAuthorizeStatus: function (res) {
            const that = this;
            const userInfo = res || getStorageSync("userInfo");
            if (!userInfo) return false;
            let checkUserInfo = that.data.checkUserInfo;
            let checkPhoneNumber = that.data.checkPhoneNumber;
            if (checkUserInfo) { //必须验证授权
                if (!userInfo.authorized) { //未授权
                    that.setData({
                        _openType: "getUserInfo",
                        loadingText: "授权中..."
                    });
                    return false
                }
            }
            if (checkPhoneNumber) { //必须验证是否注册
                if (!userInfo.phone) {
                    that.setData({
                        _openType: "getPhoneNumber",
                        loadingText: "登录中..."
                    });
                    return false
                }
            }
            that.setData({
                show: !that.data.position,
                _openType: "",
                loadingText: that.data._loadingText
            })
            return true
        },


        /*
        * 授权用户头像信息
        * */
        getUserInfo: async function (e) {
            try {
                let userInfo = e.detail.userInfo || "";
                let position = this.data.position;
                if (userInfo) {
                    position ? Toast.loading({
                        mask: true,
                        context: this,
                        message: "获取信息中...",
                        selector: "#van-toast_authorize",
                    }) : this.setData({loading: true});
                    let res = await updateUsrInfo(Object.assign(userInfo, {authorized: true}));
                    this.triggerEvent("authorizeBtn", {name: "getUserInfo", result: res}, {});
                    this.checkAuthorizeStatus(res);
                    position ? Toast.clear() : this.setData({loading: false});
                }
            } catch (e) {
                Toast.fail({
                    mask: true,
                    context: this,
                    message: "获取授权信息失败！",
                    selector: "#van-toast_authorize",
                });
            }
        },

        /*
        * 注册 绑定手机号
        * */
        getPhoneNumber: async function (e) {
            try {
                let cloudID = e.detail.cloudID;
                let position = this.data.position;
                if (cloudID) {
                    position ? Toast.loading({
                        mask: true,
                        context: this,
                        message: "获取信息中...",
                        selector: "#van-toast_authorize",
                    }) : this.setData({loading: true});
                    let phoneNumber = await getPhoneNumber(cloudID);
                    this.triggerEvent("authorizeBtn", {name: "getUserInfo", result: phoneNumber}, {});
                    this.checkAuthorizeStatus(Object.assign(getStorageSync("userInfo"), {phone: phoneNumber}));
                    position ? Toast.clear() : this.setData({loading: false});
                }
            } catch (e) {
                Toast.fail({
                    mask: true,
                    context: this,
                    message: "获取授权信息失败！",
                    selector: "#van-toast_authorize",
                });
                console.error(e)
            }
        },

        /*
        * 点击按钮验证
        * */
        bindClick: function () {
            if (this.checkAuthorizeStatus()) {
                this.triggerEvent("authorizeBtn", {name: "click", result: getStorageSync("userInfo")}, {});
            }
        }
    }
})
